﻿using FastORM.Entity;
using FastORM.Enum;
using FastORM.Handle;
using FastUtil.Convert;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace FastORM.Applicaiton
{
    public class CommonDao
    {
        public string ConnectionString { set; get; }
        public DataBaseClass DataBaseClass { set; get; }
        public CommonDao()
        {

        }
        public CommonDao(DataBaseClass DataBaseClass, string ConnectionString)
        {
            this.DataBaseClass = DataBaseClass;
            this.ConnectionString = ConnectionString;
        }
        public void Insert<T>(T Entity) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            Session.Insert(Entity);
        }
        public void InsertBatch<T>(List<T> EntityList, int BufferSize = 500) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            Session.InsertBatch(EntityList, BufferSize);
        }
        public void Delete<T>(T Entity) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            Session.Delete(Entity);
        }
        public void Delete<T>(string KeyFieldValue) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            Session.Delete<T>(KeyFieldValue);
        }
        public void Delete<T>(string FieldName, object FieldValue) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            Session.Delete<T>(FieldName, FieldValue);
        }
        public void Update<T>(T Entity) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            Session.Update<T>(Entity);
        }

        public void Update<T>(T Entity,string KeyFiledName) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            Session.Update<T>(Entity, KeyFiledName);
        }

        public void Update<T>(T Entity, string[] UpdateFileds) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            Session.Update<T>(Entity, UpdateFileds);
        }
        public T Find<T>(string KeyFieldValue) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            return Session.Find<T>(KeyFieldValue);
        }
        public T Find<T>(string FieldName, object FieldValue) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            return Session.Find<T>(FieldName, FieldValue);
        }
        public List<T> FindList<T>(string SQL, List<ParamEntity> Params = null) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            return Session.FindList<T>(SQL, Params);
        }
        public List<T> FindList<T>(string Columns, string Where, string OrderBy, List<ParamEntity> Params = null) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            return Session.FindList<T>(Columns, Where, OrderBy, Params);
        }
        public List<T> FindPageList<T>(string Columns, string Where, string OrderBy, int PageIndex, int PageSize, List<ParamEntity> Params = null) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            return Session.FindPageList<T>(Columns, Where, OrderBy, PageIndex, PageSize, Params);
        }
        public List<T> FindPageList<T>(string SQL, int PageIndex, int PageSize, List<ParamEntity> Params = null) where T : BaseEntity
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            return Session.FindPageList<T>(SQL, PageIndex, PageSize, Params);
        }
        public DataTable FindViewList(string SQL, List<ParamEntity> Params = null)
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            return Session.FindViewList(SQL, Params);
        }
        public DataTable FindViewPageList(string SQL, int PageIndex, int PageSize, List<ParamEntity> Params = null)
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            return Session.FindViewPageList(SQL, PageIndex, PageSize, Params);
        }
        public void ExecuteSQL(string SQL, List<ParamEntity> Params = null)
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            Session.ExecuteSQL(SQL, Params);
        }
        public object ExecuteScar(string SQL, List<ParamEntity> Params = null)
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            return Session.ExecuteScar(SQL, Params);
        }
        public string ExecuteToString(string SQL, List<ParamEntity> Params = null)
        {
            return ValueConvert.ConvertToString(ExecuteScar(SQL, Params));
        }
        public int ExecuteToInt(string SQL, List<ParamEntity> Params = null)
        {
            return ValueConvert.ConvertToInt32(ExecuteScar(SQL, Params));
        }
        public decimal ExecuteToDecimal(string SQL, List<ParamEntity> Params = null)
        {
            return ValueConvert.ConvertToDecimal(ExecuteScar(SQL, Params));
        }
        public double ExecuteToDouble(string SQL, List<ParamEntity> Params = null)
        {
            return ValueConvert.ConvertToDouble(ExecuteScar(SQL, Params));
        }
        public DateTime ExecuteToDateTime(string SQL, List<ParamEntity> Params = null)
        {
            return ValueConvert.ConvertToDateTime(ExecuteScar(SQL, Params));
        }
        public bool ExecuteTransition(Action TransitionAction, out string ExceptionStr, bool AutoRollBack = true)
        {
            SqlSession Session = SqlSessionFactory.OpenSession(DataBaseClass, ConnectionString);
            return Session.ExecuteTransition(TransitionAction, out ExceptionStr, AutoRollBack);
        }
    }
}
